/**
 * \file ActiveObject.cpp
 * \brief Active Object source cpp
 * \author Edouard ROGE
 * \date 04 May 2014
 */
#include "ActiveObject.h"

namespace Core
{
	ActiveObject::~ActiveObject()
	{
		VoidFunctionVoid quitToken = std::bind(&ActiveObject::Done, this);
		Send(quitToken);
		m_thread.join();
	}

	ActiveObjectUPtr ActiveObject::CreateActiveObject()
	{
		ActiveObjectUPtr activeObject(new ActiveObject());
		activeObject->m_thread = std::thread(&ActiveObject::Run, activeObject.get());
		return activeObject;
	}

	void ActiveObject::Send(VoidFunctionVoid _message)
	{
		m_MessageQueue.Push(_message);
	}

	ActiveObject::ActiveObject() : m_bDone(false)
	{

	}

	ActiveObject::ActiveObject(const ActiveObject & /*_other*/)
	{

	}

	ActiveObject & ActiveObject::operator = (const ActiveObject & /*_other*/)
	{
		return *this;
	}

	void ActiveObject::Run()
	{
		while (!m_bDone)
		{
			VoidFunctionVoid func;
			m_MessageQueue.WaitAndPop(func);
			func();
		}
	}

	void ActiveObject::Done()
	{
		m_bDone = true;
	}
}